// pages/setPassword/setPassword.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneValue: '',
    mobile: '',
    codeVal: '',
    codeText: '获取验证码',
    time: 60,
    bg: '#d71d06',
    again: false,
    win: true,
    blurPassword: null,
    blurAffirmPassword: null
  },
  // 获取手机号
  blurPhone(e) {
    const that = this;
    that.setData({
      phoneValue: e.detail.value
    })

  },
  // 获取验证码
  blurCode(e) {
    const that = this;
    that.setData({
      codeVal: e.detail.value
    })
  },
  //验证手机号获取验证码
  code() {
    const that = this;
      let token = app.globalData.token;
      let json = {
        scene: 6,
        unique_id: token,
        mobile: that.data.mobile
      }
      let jsonData = app.utils.getSignData(json);
      app.utils.post(app.api.reqUrls.phone_code, jsonData).then(res => {
        console.log(res)

        if (res.status == 1) {
          this.countDown();
          that.setData({
            again: true,
            codeText: '秒后在获取',
            bg: "#999999"
          })
        } else {
          throw res.msg;
        }
      }).catch(err => {
        that.setData({
          hiddenLoading: true
        })
        app.showInfo(err.toString())
      })
  },
  //倒计时
  countDown() {
    const that = this;
    if (that.data.again) {
      return false;
    } else {
      let time = 60;
      const timeCenter = setInterval(function() {
        time--;
        if (time <= 0) {
          time = 60;
          that.setData({
            again: false,
            codeText: '获取验证码',
            bg: "#d71d06",
            time: 60
          })
          clearInterval(timeCenter)
        } else {
          that.setData({
            time: time
          })
        }
      }, 1000)
    }
  },
  // 输入验证码点击确认
  affirm() {
    const that = this;

    let token = app.globalData.token;
    let json = {
      check_code: that.data.codeVal,
      unique_id: token,
      mobile: that.data.mobile
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.phoneVerify, jsonData).then(res => {
      console.log(res)
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        that.setData({
          win: false
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 2000
        })
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })

  },
  //密码
  blurPassword(e) {
    const that = this;
    that.setData({
      blurPassword: e.detail.value
    })

  },
  //确认密码
  blurAffirmPassword(e) {
    const that = this;
    that.setData({
      blurAffirmPassword: e.detail.value
    })

  },
  // 输入密码点击确认
  submit() {
    const that = this;
    var blurPassword = that.data.blurPassword;
    var blurAffirmPassword = that.data.blurAffirmPassword;


    let pages = getCurrentPages();
    let currPage = null; //当前页面
    let prevPage = null; //上一个页面

    if (pages.length >= 2) {
      currPage = pages[pages.length - 1]; //当前页面
      prevPage = pages[pages.length - 2]; //上一个页面
    }

    if (blurPassword == null || blurPassword == '') {
      wx.showToast({
        title: '请输入新密码',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (blurAffirmPassword == null || blurAffirmPassword == '') {
      wx.showToast({
        title: '请再次输入新密码',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (blurPassword !== blurAffirmPassword) {
      wx.showToast({
        title: '输入的密码不一致',
        icon: 'none',
        duration: 2000
      })
    } else {
      //提交给后台
      let token = app.globalData.token;
      let json = {
        token,
        new_password: app.utils.passwordMd5(that.data.blurAffirmPassword)
      }
      let jsonData = app.utils.getSignData(json);
      app.utils.post(app.api.reqUrls.setPassword, jsonData).then(res => {
        if (res.status == 1) {
          wx.showToast({
            title: res.msg,
            icon: 'success',
            duration: 2000
          })
          setTimeout(() => {
            if (prevPage && that.data.pay) {
              prevPage.setData({
                pwd: that.data.blurAffirmPassword
              });
              wx.navigateBack({
                delta: -1,
                success(){
                  prevPage.submitOrder();
                }
              })
            } else {
              wx.navigateBack({
                delta: -1
              })
            }
          }, 1500)
          console.log(res)
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      }).catch(err => {

        app.showInfo(err.toString())
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;

    
    const token = app.globalData.token;
    that.setData({
      pay: options.where == 'fillOrder'
    })
    let json = {
      token
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.getSet, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result.mobile == '') {
          wx.redirectTo({
            url: '/page/tabBar/bindphone/bindphone',
          })
        }else{
          that.setData({
            mobile: res.result.mobile
          })
        }
      } else {
        throw res.msg
      }
      console.log(res)
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },

})